package cn.market.service.ad.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import cn.market.bean.QueryResult;
import cn.market.bean.ad.HtmlModule;
import cn.market.service.ad.HtmlModulService;
import cn.market.service.base.DaoSupport;

@Service
public class HtmlModuleServiceBean extends DaoSupport<HtmlModule> implements
		HtmlModulService {

	@SuppressWarnings("unchecked")
	@Override
	public QueryResult<HtmlModule> getAllHtmlModules(String where, int first,
			int max, Object[] params, LinkedHashMap<String, String> orderby) {
		String sql = "select modulId,name,visible,money from htmlmodule "
				+ where + super.buildOrderby(orderby);
		SQLQuery query = super.getSession().createSQLQuery(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(String.valueOf(i + 1), params[i]);
			}
		}
		if (first != -1 && max != -1) {
			query.setFirstResult(first).setMaxResults(max);
		}
		List list = query.list();
		QueryResult<HtmlModule> qr = new QueryResult<HtmlModule>();
		List<HtmlModule> ms = new ArrayList<HtmlModule>();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				HtmlModule m = new HtmlModule();
				m.setModulId(new Integer(String.valueOf(obj[0])));
				m.setName(String.valueOf(obj[1]));
				m.setVisible(Boolean.valueOf(String.valueOf(obj[2])));
				m.setMoney(Float.valueOf(String.valueOf(obj[3])));
				ms.add(m);
			}
		}
		qr.setResultlist(ms);
		qr.setTotalrecord(super.getCount());
		return qr;
	}

	@SuppressWarnings("unchecked")
	@Override
	public HtmlModule getHtmlModuleById(Integer id) {
		String sql = "select modulId,name,visible,money from htmlmodule where modulId=:1";
		SQLQuery query = super.getSession().createSQLQuery(sql);
		query.setParameter("1", id);
		List list = query.list();
		HtmlModule m = new HtmlModule();
		if (list != null && list.size() > 0) {
			Object[] obj = (Object[]) list.get(0);
			m.setModulId(new Integer(String.valueOf(obj[0])));
			m.setName(String.valueOf(obj[1]));
			m.setVisible(Boolean.valueOf(String.valueOf(obj[2])));
			m.setMoney(Float.valueOf(String.valueOf(obj[3])));
		}
		return m;
	}

	@Override
	public void updateHtmlModule(HtmlModule module) {
		// TODO Auto-generated method stub

	}

}
